import SplashComponent from "@components/Splash/SplashComponent";
import AppSettings from "@runtime/base/AppSettings";
import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";
import { Ref, ref, WatchStopHandle } from "@vue/runtime-dom";

/**
 * 起始页
 */
export default class HomeContent {
    private static instance: HomeContent = new HomeContent();
    /**
     * 起始页
     */
    public static get Default(): HomeContent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);

        for (const watchStopHandle of this.watchStopHandles) {
            watchStopHandle();
        }
        this.watchStopHandles.clear();
    }

    //项目类型
    public itemType: Ref<string> = ref('');

    // 设备连接信息
    public Message: Ref<any> = ref('服务器已断开，请重新连接服务器');
    // 获取的车辆设备
    public deviceName: Ref<any> = ref('');

    public TypeStatus: Ref<boolean> = ref(false);
    // 类型（考核还是训练）
    public ApplicationType: Ref<any> = ref();
    // 时间
    public Duration: Ref<any> = ref();
    // 倒计时控制
    public timerControl: Ref<any> = ref()

    //存贮启动参数
    public CacheFullData: Ref<any> = ref()
    // 平台和本地返回和选择故障类页面是否显示
    public showType: Ref<boolean> = ref(false)


    public Loaded: Ref<boolean> = ref(false);

    public ProgressText: Ref<string> = ref('');
    public ProgressVal: Ref<number> = ref(0);
    //传感器显隐
    public sensorType: Ref<boolean> = ref(true);
    // public MultimeterData: Ref<ClientMessage.StructureCognitionResponse> = ref(new ClientMessage.StructureCognitionResponse)

    // 旧-弃用语音控制
    public VoiceControl: Ref<boolean> = ref(true);
    public pausableType: Ref<any> = ref({ pausable: true });
    public endplay: Ref<any> = ref({ ended: true });
    // public endplay: Ref<boolean> = ref(false);

    // 项目名称
    public ProjectName: Ref<string> = ref('');

    // 新-语音控制
    public ispPausePlay: Ref<boolean> = ref(false);

    private watchStopHandles: Array<WatchStopHandle> = [];

    public async Awake(mode:any): Promise<void> {
        this.Loaded.value = true;
        this.ProgressText.value = '渲染引擎初始化'
        this.ProgressVal.value = 85

        // 启动Unity
        // 资源编号R00042
        const configuration = JSON.stringify({
            LocalAddress: AppSettings.Default.LocalAddress,
            ShortName: 'P10061',
            Type: 10,
            Mode: mode,
        });

        await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.EmbeddedApplicationLaunchRequest('../EmbeddedApplication/Unity/Mix.Unity.exe', configuration));
        setTimeout(async () => {
            this.ProgressText.value = '环境模块初始化'
            this.ProgressVal.value = 70
        }, 800);
        // // 获取Unity版本状态
        await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.EmbeddedApplicationVersionRequest());
        setTimeout(async () => {
            this.ProgressText.value = '相机模块初始化'
            this.ProgressVal.value = 45
        }, 1600);
    }

    public async Destroy(): Promise<void> {
    }
}